home *** CD-ROM | disk | FTP | other *** search
/ Amiga Collections: Panorama / Panorama - Disk 17C (1987-05)(Pacific North-West Amigas Club)[WB].zip / Panorama - Disk 17C (1987-05)(Pacific North-West Amigas Club)[WB].adf / Icon.Doc < prev    next >
Text File  |  1987-05-25  |  7KB  |  170 lines

  1. Icon is a very comprehensive computer programming language developed
  2. under a National Science Foundation Grant by Ralph E. Griswold.  It 
  3. has been stored in packed format in the directory Icon so there would
  4. be room for some other material on the disk.  To restore the file, first
  5. format a new disk called "Icon" with the AmigaDos command:
  6.  
  7.     format drive df0: name "Icon" noicons
  8.  
  9. Make Icon the current directory, and execute the script file "MakeIcon".
  10. Sorry, but this procedure will require two drives.  If you only have a
  11. single drive system, and can't twist a two-drive friend's arm, then I
  12. suggest you come to the next PD party next June 12, 1987.  An unpacked
  13. version of Icon will be available there.
  14.  
  15. Below is a short example by club Guru, Larry Phillips that will introduce
  16. you to Icon.
  17.  
  18. -Club Librarian,
  19.  Jeff Lydiatt. 
  20.  
  21. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
  22.  
  23.  
  24.  
  25.                          Programming in Icon
  26.                        A Simple Search Program
  27.                      (C) 1987 by Larry Phillips
  28.  
  29.   I decided to write a search program that mimics the Amiga supplied SEARCH to
  30. see how Icon compared to other languages. The minimal search, one that does not
  31. print out the line numbers, and which is case sensitive, is as follows:
  32.  
  33. procedure main(args)
  34.   in := open (args[1]) | stop("Can't open input file")
  35.   pattern := args[2]
  36.   while line := read(in) do
  37.     if find (pattern,line) then
  38.       write(line)
  39.     else next
  40. end
  41.  
  42. A few words of explanation are in order.
  43.  
  44. Arguments are passed as a list of strings. args[1] is the first item in the
  45. list, and is the file name. The line that opens the file assigns the file
  46. handle to the variable 'in', if successful. If not successful, the program will
  47. stop with a message, "Can't open input file." An "english" reading of the line
  48. would be "Either open the file or stop and print a message". Note that even
  49. though I opened the file, I have not closed it anywhere. This is because all
  50. files are closed when the program terminates. If you are working with a large
  51. number of files, it is good practice to close them as they become unnecessary.
  52.  
  53. The second element of the argument list is the search string. I have added the
  54. extra assignment of the search string to the variable 'pattern' for clarity
  55. only, and could as easily have used 'args[2]' later on in the 'if find..'
  56. statement.
  57.  
  58. The 'while..' statement will read lines from the specified file until either
  59. EOF or an error condition is encountered, and will assign the value read to the
  60. variable 'line'. The while statement operates much like the while in C, in that
  61. it will execute the next line until the terminating condition is met. Note that
  62. the 'if' statement following the 'while' includes the 'write', which is on the
  63. next line for clarity only. It could have been written as:
  64.  
  65. if find (pattern,line) then write (line)
  66.  
  67. The 'find' function returns the position of one string within another. In this
  68. case we are only interested in the success or failure of the 'find' statement,
  69. and if successful, we write the line to the screen. If it fails, the 'else'
  70. statement executes and the 'next' takes us back, continuing with the 'while'
  71. statement.
  72.  
  73. The 'end' tells us that this is the end of the procedure.
  74.  
  75. Since we might be interested in the line number in which any matches occur, and
  76. since we want to make the search case insensitive, I modified the program to
  77. handle these criteria. Now bear in mind that I am strictly a novice Icon
  78. programmer, and that there are undoubtedly better ways to do this. The modified
  79. code looks like this:
  80.  
  81. procedure main(args)
  82.   i := 0
  83.   in := open (args[1]) | stop("Can't open input file")
  84.   pattern := args[2]
  85.   while line := read(in) do {
  86.     i +:= 1
  87.     if find (pattern,map(line,&ucase,&lcase)) then
  88.       write(i || " " || line)
  89.     else next
  90.     }
  91. end
  92.  
  93. Notice that that we are using a variable to count lines. The use of the
  94. variable 'i' is straightforward, but notice that we now have two statements to
  95. perform within the 'while' control block. The curly brace is used to logically
  96. group the lines as part of the 'while' statement. It's exactly the same as in a
  97. C program.
  98.  
  99. The case insensitivity is provided by modifying the 'find'. The 'map' function
  100. will cause any characters in 'line' that appear in the second string to be
  101. replaced with the corresponding characters in the third string. As an example,
  102. the statement:
  103.  
  104. write map("hello there","e","-")
  105.  
  106. will cause the string "h-llo th-r-" to be written to the screen.
  107.  
  108. The mapping strings in this case are special strings called 'csets', which are
  109. sets of unique characters. An example of a cset would be the set of vowels, and
  110. can be generated with the statement:
  111.  
  112. vowel := 'aeiou'
  113.  
  114. The exact same set can be made with either:
  115.  
  116. vowel := 'uoiea' 
  117.  
  118.   or
  119.  
  120. vowel := 'aaaeeeiiiooouuu'
  121.  
  122. Since the characters are unique, and since the idea of order in a cset in
  123. meaningless, the same cset is produced in all three cases. In the example
  124. program, there are two csets used, &lcase and &ucase. These are "built in"
  125. csets that represent the set of all lower case characters and the set of all
  126. upper case characters, respectively.
  127.  
  128. The function 'map(line,&ucase,&lcase)' will return a string that consists of
  129. whatever is contained in the variable 'line', but with all the upper case
  130. characters mapped to lower case. The 'find' function then, will perform its
  131. function on the mapped string. To be truly general, and to allow the search
  132. string to be in mixed case, 'map' should be applied to the variable 'pattern'.
  133. This is best done outside the loop to save time, since it nees to be done only
  134. once. To implement this, we would change the line 'pattern := args[2]' to read
  135. 'pattern := map (args[2],&ucase,&lcase)'.
  136.  
  137. The last change is in the line we write to the screen when the find is
  138. successful. The '||' operator is the operator to concatenate strings.
  139. Note that due to Icon's automatic type conversion, we do not have to
  140. explicitly convert the variable 'i', which we have been using as a numeric
  141. counter, into a string. The type is converted for us, and the resulting line
  142. will consist of the line number followed by a space, followed by the line
  143. itself.
  144.  
  145. So, that's it. Now for the burning questions. How big is the resulting code,
  146. and how is the speed? How long does it take to compile?
  147.  
  148. Icon is not a true "compiled" langauge, nor is it a true "iterpreted" language.
  149. The size of the resulting program is only 3484 bytes, but when executed, the
  150. run-time module (iconx) must be loaded in, and it is nearly 100K. On a 1.5 meg
  151. system, with Icon kept in RAM:, the compile speed is lightning fast, taking
  152. (for this program) just over 2 seconds.
  153.  
  154. The speed is surprising for such a high level language. I have a file I search
  155. on a regular basis that is about 120K. For the test, I used a stopwatch, and
  156. copied the Amigados SEARCH command into RAM: so that load times would not be
  157. dependent upon device speed. The results were:
  158.  
  159. Amigados SEARCH -- about 51 seconds
  160. Icon search     -- about 39 seconds
  161.  
  162. Note that the Icon search command, if you had it on disk alnong with 'iconx',
  163. would have taken a longer to load, and the results would have been a lot
  164. closer.
  165.  
  166. I feel that as a high level language, Icon is a very respectable language for
  167. "quick and dirty" or "one time" programs, especially in the area of text
  168. processing.  The price is right (it's PD), and it's relatively easy to learn.
  169.  
  170.